<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Upsanddowns\Controller;
use Think\Controller;
use Common\Api\SendSMSApi;
/**
 * 前台土特产活动控制器
 */
class NativeController extends BaseController {
	//土特产活动发起页面
	public function index() {
        $user_auth= cookie('user_auth');
        $user_id= $user_auth['uid'];
     // $user_id=418;
        //如果接收到了用户id就为接收的用户id，否则为当前用户的id
        $user_id=I('get.uid',0,'trim')?base64_decode(I('get.uid',0,'trim')):$user_id;
        $where['user_id'] = $user_id;
        $where['type'] = 1;
        //$info=M('native_products_activity')->where("user_id='".$user_id."'")->find();
        $info=M('native_products_activity')->where($where)->find();
        if($info){//发起成功后的页面，用于分享
            //微信分享地址
            $link="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'&uid='.base64_encode($user_id);
            $this->link= U('Native/index',array('uid'=>base64_encode($user_id)));
            $this->imgUrl="http://".$_SERVER['HTTP_HOST'].get_cover($info['cover_id'],'path');
            $this->assign('info',$info);
            $tpl="content";
        }else{//发起活动页面
            //令牌验证，防止远程提交  可注释不用
            $check_token = md5(uniqid(rand(),true));
            session('check_token',$check_token.$user_id);
            $this->assign('check_token',$check_token);
            $tpl="index";
        }
        $this->display($tpl);
	}
    public function content(){
        $this->display();
    }

    /**
     * 发送短信验证码
     */
    public function send_verify(){
        $mobile=I('post.mobile');
        $sendsms=new SendSMSApi();
        $sendsms->sendCode($mobile);
    }

    /**
     * 生成土特产活动
     */
    public function add(){

       $user_auth= cookie('user_auth');
       $user_id= $user_auth['uid'];
       //$user_id=418;
       $result=array('status'=>1,'message'=>'','data'=>'');
        $end_time=mktime(12,00,00,2,22,2016);
        if(time()>$end_time){
            $result['message']='对不起，活动已截止';
            $this->ajaxReturn ($result,'JSON');
        }
       $checktoken=I('post.checktoken','','trim');
        //安全验证
       if($checktoken.$user_id!=session('check_token')){
            //$result['message']='令牌验证失败,请刷新页面重试';
            //$this->ajaxReturn ($result,'JSON');
       }
       $where['user_id'] = $user_id;
       $where['type'] = 1;
       if(M('native_products_activity')->where($where)->count()){
           $result['message']='每个微信号只能发起一次';
           $this->ajaxReturn ($result,'JSON');
       }
       $code=I('post.code','','intval');
       $data['mobile']=I('post.mobile');
       $sendsms=new SendSMSApi();
       if(!$sendsms->checkCode($data['mobile'],$code)){

           $result['message']='验证码不正确';
           $this->ajaxReturn ($result,'JSON');
       }
       $data['native_products']=I('post.native_products','','trim');
        if(!$data['native_products']){
            $result['message']='土特产名称不能为空';
             $this->ajaxReturn ($result,'JSON');
        }
       $data['cover_id']=I('post.cover_id',0,'intval');
        if(!$data['cover_id']){
            $result['message']='请上传图片';
             $this->ajaxReturn ($result,'JSON');
        }
       $data['province']=I('post.province','','trim');
       $data['city']=I('post.city','','trim');
       $data['status']=1;
       $data['type']=1;
       $data['add_time']=time();
       $data['user_id']=$user_id;
       if(M('native_products_activity')->add($data)){
           $result['status']=0;
           $result['message']='成功,快分享给好友吧！';
       }else{
           $result['message']='失败';
       }
       $this->ajaxReturn ($result,'JSON');

    }

    //支持
    public function support(){

        $user_auth= cookie('user_auth');
        $user_id= $user_auth['uid'];
       // $user_id=510;
        $result=array('status'=>0,'message'=>'','data'=>'');
        $end_time=mktime(12,00,00,2,22,2016);
        if(time()>$end_time){
            $result['status']=1;
            $result['message']='对不起，活动已截止';
            $this->ajaxReturn ($result,'JSON');
        }
        $where['user_id']=$user_id;
        $where['activity_id']=I('post.id',0,'intval');
        if(M('native_products_support')->where($where)->count()){
            $result['status']=1;//点过
            $result['message']='大过年的，你也推荐一个家乡的味道呗，获赞一定更多!<img width="20" height="20" src="'.__ROOT__.'/Public/Upsanddowns/images/dian_z.png" id="img_zd" >';
            $this->ajaxReturn ($result,'JSON');
        }
        $where['add_time']=time();
        if($id=M('native_products_support')->add($where)){
            M('native_products_activity')->where("id=".$where['activity_id'])->setInc('support_num',1);
            $result['message']='大过年的，你也推荐一个家乡的味道呗，获赞一定更多!<img width="20" height="20"  src="'.__ROOT__.'/Public/Upsanddowns/images/dian_z.png" id="img_zd" >';//成功
        }else{
            $result['status']=1;//失败
            $result['message']='失败';
        }
        $this->ajaxReturn ($result,'JSON');

    }



}



